
function getRootPath(){  
    //获取当前网址，如： http://localhost:8088/test/test.jsp  
    var curPath=window.document.location.href;  
    //获取主机地址之后的目录，如： test/test.jsp  
    var pathName=window.document.location.pathname;  
    var pos=curPath.indexOf(pathName);  
    //获取主机地址，如： http://localhost:8088  
    var localhostPaht=curPath.substring(0,pos);  
    //获取带"/"的项目名，如：/test  
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1); 
    return(localhostPaht+projectName);  
} 

//检验商品名称
function checkProductName(){
	var flag = false;
	var name = $("#productName").val();
	name = name.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	name = name.replace(/\t/g," ");//去制表符
	if (name == null || name == "") {
		$("#checkProductName").html("请输入商品名称");
	} else if (name.length > 50) {
		$("#checkProductName").html("输入商品名称过长");
	} else {
		$("#checkProductName").html("*");
		flag = true;
	}
	$("#name").val(name);
	return flag;
}
//检验商品名称
function checkFullName(){
	var flag = false;
	var fullName = $("#fullName").val();
	fullName = fullName.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if (fullName == null || fullName == "") {
		$("#checkFullName").html("请输入商品全称");
	} else if (fullName.length > 100) {
		$("#checkFullName").html("输入商品全称过长");
	} else {
		$("#checkFullName").html("*");
		flag = true;
	}
	$("#fullName").val(fullName);
	return flag;
}
//检验商品价格
function checkPrice() {
	var flag = false;
	var num = /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;//验证金额
	var price = $("#price").val();
	price = price.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if (price == null || price == "") {
		$("#checkPrice").html("请输入价格");
	} else if (!price.match(num)) {
		$("#checkPrice").html("请输入合法金额");
	} else if (price > 100000000) {
		$("#checkPrice").html("输入金额过大");
	} else {
		$("#point").html(price);
		$("#checkPrice").html("");
		flag = true;
	}
	return flag;
	
}
//检验支付方式
function checkPayMethod(){
	var payMethod = $("#payMethod").val();
	if(payMethod == null || payMethod == ""){
		$("#checkPrice").html("请选择货币类型");
		return false;
	}else {
		$(".money").html($("#payMethod").attr("currencycode"));
		$("#checkPrice").html("");
		return true;
	}
}
//检验市场价格
function checkMarketPrice(){
	var flag = false;
	var num = /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;//验证金额
	var marketPrice = $("#marketPrice").val();
	marketPrice = marketPrice.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(marketPrice != null && marketPrice != ""){
		if(!marketPrice.match(num)){
			$("#checkMarketPrice").html("请输入合法数字");
		}else if (marketPrice > 100000000){
			$("#checkMarketPrice").html("输入金额过大");
		}else {
			$("#checkMarketPrice").html("");
			flag = true;
		}
	}else{
		$("#checkMarketPrice").html("");
		flag = true;
	}
	$("#marketPrice").val(marketPrice);
	return flag;
}
//检验商品图片
function checkImage(){
	var image = $("#image").val();
	if(image == null || image == "" || image == getRootPath()+"/images/product/main.png"){
		$("#checkImage").html("请选择图片");
		return false;
	}else{
		$("#checkImage").html("");
		return true;
	}
}
//验证规格swf
function checkSwf(){
	var tt=true;
	$(".specSwf").each(function(){
		if($(this).html()=="（请上传swf文件）*" && $(this).parent().css("display")=="inline"){
			tt = false;
		}
	});
	return tt;
}
//检验备注
function checkRemark(){
	var flag = false;
	var remark = $("#remark").val();
	remark = remark.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(remark != null && remark.length > 255){
		$("#checkRemark").html("输入字数过多");
	}else{
		$("#checkRemark").html("");
		flag = true;
	}
	$("#remark").val(remark);
	return flag;
}
//检验关键词
function checkKeyword(){
	var flag = false;
	var keyword = $("#keyword").val();
	keyword = keyword.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(keyword != null && keyword.length > 25){
		$("#checkKeyword").html("输入字数过多");
	}else{
		$("#checkKeyword").html("");
		flag = true;
	}
	$("#keyword").val(keyword);
	return flag;
}
//检验SEO 描述
function checkSeodescription(){
	var flag = false;
	var seodescription = $("#seodescription").val();
	seodescription = seodescription.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(seodescription != null && seodescription.length > 25){
		$("#checkSeodescription").html("输入字数过多");
	}else{
		$("#checkSeodescription").html("");
		flag = true;
	}
	$("#seodescription").val(seodescription);
	return flag;
}
//检验SEO 关键词
function checkSeokeywords(){
	var flag = false;
	var seokeywords = $("#seokeywords").val();
	seokeywords = seokeywords.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(seokeywords != null && seokeywords.length > 25){
		$("#checkSeokeywords").html("输入字数过多");
	}else{
		$("#checkSeokeywords").html("");
		flag = true;
	}
	$("#seokeywords").val(seokeywords);
	return flag;
}
//检验SEO 标题
function checkSeotitle(){
	var flag = false;
	var seotitle = $("#seotitle").val();
	seotitle = seotitle.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(seotitle != null && seotitle.length > 25){
		$("#checkSeotitle").html("输入字数过多");
	}else{
		$("#checkSeotitle").html("");
		flag = true;
	}
	$("#seotitle").val(seotitle);
	return flag;
}
//检验成本价
function checkCost(){
	/*var flag = false;
	var num = /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;//验证金额
	var cost = $("#cost").val();
	cost = cost.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(cost != null && cost != ""){
		if(!cost.match(num)){
			$("#checkCost").html("请输入合法数字");
		}else if (cost > 100000000){
			$("#checkCost").html("输入金额过大");
		}else {
			$("#checkCost").html("");
			flag = true;
		}
	}else{
		$("#checkCost").html("");
		flag = true;
	}
	$("#cost").val(cost);
	return flag;*/
	return true;
}
//检验计价单位
function checkUnit(){
	/*var flag = false;
	var unit = $("#unit").val();
	unit = unit.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(unit != null && unit.length > 25){
		$("#checkUnit").html("输入字数过多");
	}else{
		$("#checkUnit").html("");
		flag = true;
	}
	$("#unit").val(unit);
	return flag;*/
	return true;
}
//检验商品重量
function checkWeight(){
	var flag = false;
	var num = /^(?:[1-9][0-9]*\.[0-9]{0,2}|0\.(?!0+$)[0-9]{0,3})$/;//验证正浮点数
	var inum = /^[1-9]*[1-9][0-9]*$/;//验证正整数
	var weight = $("#weight").val();
	weight = weight.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(weight != null && weight != ""){
		if(!(weight.match(num)||weight.match(inum)||weight==0)){
			$("#checkWeight").html("请输入合法数字(保留三位小数)");
		}else if (weight > 100000000){
			$("#checkWeight").html("输入重量过大");
		}else {
			$("#checkWeight").html("");
			flag = true;
		}
	}else{
		$("#checkWeight").html("");
		$("#weight").val(0.000);
		flag = true;
	}
	$("#weight").val(weight);
	return flag;
}
//检验库存
function checkStock(){
	var num = /^[1-9]*[1-9][0-9]*$/;//验证整数
	var stock = $("#stock").val();
	stock = stock.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(stock == null || stock == ""){
		$("#checkStock").html("请输入库存数量");
		return false;
	}else if (!stock.match(num)){
		$("#checkStock").html("请输入合法数字");
		return false;
	}else if (stock > 10000) {
		$("#checkStock").html("输入库存过大");
		return false;
	}else{
		$("#checkStock").html("");
		return true;
	}
}
//检验库存备注
function checkStockMemo(){
	/*var flag = false;
	var stockMemo = $("#stockMemo").val();
	stockMemo = stockMemo.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(stockMemo != null && stockMemo.length > 255){
		$("#checkStockMemo").html("输入字数过多");
	}else{
		$("#checkStockMemo").html("");
		flag = true;
	}
	$("#stockMemo").val(stockMemo);
	return flag;*/
	return true;
}
//检验商品表格库存、价格
function checkTable(row){
	//返回值
	if(checkTableStock(row) && checkTablePrice(row)){
		return true;
	}else{
		return false;
	}
}
//检验商品表格库存
function checkTableStock(row){
	var stockFlag = false;
	var num = /^[1-9]*[1-9][0-9]*$/;//验证整数
	var stock = $("#stock_"+row).val();//表格库存
	stock = stock.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	//验证库存
	if(checkHasSpecification()){
		if(stock == null || stock == ""){
			$("#checkTable_"+row).html("请输入库存数量");
		}else if (!stock.match(num)){
			$("#checkTable_"+row).html("请输入合法数字");
		}else if (stock > 10000) {
			$("#checkTable_"+row).html("输入库存过大");
		}else{
			$("#checkTable_"+row).html("*");
			stockFlag = true;
		}
	}else{
		stockFlag = true;
	}
	getStock();
	return stockFlag;
}
//检验商品表格价格
function checkTablePrice(row){
	var priceFlag = false;
	var money = /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;//验证金额
	var price = $("#price_"+row).val();//表格价格
	price = price.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	//验证价格
	if(checkHasSpecification()){
		if(price == null || price == ""){
			$("#checkTable_"+row).html("请输入商品价格");
		}else if (!price.match(money)){
			$("#checkTable_"+row).html("请输入合法数字");
		}else if (price > 100000000) {
			$("#checkTable_"+row).html("输入金额过大");
		}else{
			$("#checkTable_"+row).html("*");
			priceFlag = true;
		}
	}else{
		priceFlag = true;
	}
	getPrice();
	return priceFlag;
}
//检验运费
function checkFreight(){
	var flag = false;
	var freight = $("#freight").val();
	var money = /^(([1-9]\d{0,9})|0)(\.\d{1,2})?$/;//验证金额
	freight = freight.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(freight != null && freight != ""){
		if(!freight.match(money)){
			$("#checkFreight").html("请输入合法金额");
		}else if (freight > 100000000){
			$("#checkFreight").html("输入金额过大");
		}else{
			$("#checkFreight").html("");
			flag = true;
		}
	}else{
		$("#checkFreight").html("");
		flag = true;
	}
	return flag;
}
//检验运费备注
function checkFreightdesc(){
	var flag = false;
	var freightdesc = $("#freightdesc").val();
	freightdesc = freightdesc.replace(/(^\s*)|(\s*$)/g,"");//去首尾空格
	if(freightdesc != null && freightdesc.length > 255){
		$("#checkFreightdesc").html("输入字数过多");
	}else{
		$("#checkFreightdesc").html("");
		flag = true;
	}
	$("#freightdesc").val(freightdesc);
	return flag;
}
//检验物流公司
function checkMdelivery(){
	var mdelivery = $("#mdelivery").val();
	if(mdelivery == null || mdelivery == ""){
		$("#checkMdelivery").html("请选择物流公司");
		return false;
	}else {
		$("#checkMdelivery").html("");
		return true;
	}
}
//检验商品属性
function checkAttribute(){
	var flag = true;
	if(checkHasAttribute()){
		var count = $(".attribute").length;
		for(no = 0 ; no < count ; no ++){
			var attribute = $("#attribute_"+no).val();
			if(attribute != null && attribute != "" && attribute.length > 0){
				$("#checkAttribute_"+no).html("");
				flag = true;
			}else{
				$("#checkAttribute_"+no).html("请选择属性");
				flag = false;
			}
		}
	}
	return flag;
} 
//检验规格
function checkSpecification(){
	var flag = true;
	if(checkHasSpecification()){
		var specification = $("#price_1").val();
		if(specification != null && specification != "" && specification.length > 0){
			$("#checkSpecification").html("");
			flag = true;
		}else{
			$("#checkSpecification").html("请选择规格");
			flag = false;
		}
	}
	return flag;
} 
//商品是否有规格
function checkHasSpecification(){
	var flag = false;
	var specification = $(".specification").val();
	if(specification != null && specification.length > 0){
		flag = true;
	}else{
		flag = false;
	}
	return flag;
} 
//商品是否有属性
function checkHasAttribute(){
	var flag = false;
	var attribute = $(".attributes").val();
	if(attribute != null && attribute.length > 0){
		flag = true;
	}else{
		flag = false;
	}
	return flag;
} 
//生成商品价格
function getPrice(){
	var price = $("#price_1").val();
	$("#price").val(price);
}
//生成商品库存
function getStock(){
	var count = parseInt("0");
	var index = 0;
	$(".tableStock").each(function(){
		if($(this).val() != null && $(this).val() != ""){
			count = count + parseInt($(this).val());
		}
		index ++;
	});
	if(index != 0){
		$("#stock").val(count);
	}
}